package com.tank.dao.impl;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.tank.bean.item.Item;
import com.tank.core.TankModel;
import com.tank.dao.ItemDAO;
import com.tank.util.PageSize;
import com.tank.util.Pager;
import com.tank.util.TankUtil;

@Repository("itemDao")
public class ItemDaoImpl extends TankDaoSupport implements ItemDAO {

	public Integer save(Item item) {
		return (Integer) getHibernateTemplate().save(item);
	}

	public boolean update(Item item) {
		getHibernateTemplate().update(item);
		return true;
	}

	public boolean delete(Item item) {
		getHibernateTemplate().delete(item);
		return true;
	}

	public Item findItemById(Integer id) {
		return getHibernateTemplate().get(Item.class, id);
	}

	public List<Item> findItemList() {
		return getHibernateTemplate().find("from Item ");
	}

	public Pager findCareItemList(String type) {

		Pager pager = new Pager(PageSize.HOME_PAGE_SIZE);
		Session sess = this.getSession();
		
		Query q = null;
		Query q2 = null;
		
		if("at".equals(type)){
			  q = sess
						.createQuery("select i from Item i  left join fetch i.parent ,User u where i.class in (Item,Product,Talk,Transpond,Comment) "
								+ "and ( i  in elements(u.atItems) ) and u.id=:id   and i.hide=false order by i.id desc");

			 q2 = sess
						.createQuery("select count(i.id) from Item i ,User u where i.class in (Item,Product,Talk,Transpond,Comment) "
								+ "and ( i  in elements(u.atItems) )  and u.id=:id and i.hide=false");
				
		}else{
			
			String cond="Product,Talk,Transpond";
			if("talk".equals(type)){
				cond="Talk";
				
			}else if("product".equals(type)){
				cond="Product";
			}
			q = sess
					.createQuery("select i from Item i  left join fetch i.parent ,User u where i.class in ("+cond+") "
							+ "and ( i.author in elements(u.follows) or i.author = u) and u.id=:id and i.hide=false order by i.id desc");

				q2 = sess
					.createQuery("select count(i.id) from Item i ,User u where i.class in ("+cond+") "
							+ "and ( i.author in elements(u.follows) or i.author = u) and u.id=:id and i.hide=false");
			
		
		}
		
		Integer id = TankUtil.getCurrentUser().getId();
		q.setParameter("id", id);
		q2.setParameter("id", id);
		

		System.out.println("查询。。。。");

		pager.setRowCount(Integer.valueOf(q2.uniqueResult() + ""));
		sess.clear();
		pager.setData(pager.pageQuery(q));
		
		return pager;
	}

	public void favour(Item item) {
		this.getHibernateTemplate().bulkUpdate(
				"update Item i set i.favourCount= i.favourCount+1 where id="
						+ item.getId());

	}

	public void transpond(Item item) {
		this.getHibernateTemplate().bulkUpdate(
				"update Item i set i.transpondCount= i.transpondCount+1 where id="
						+ item.getParent().getId());
		this.getHibernateTemplate().save(item);

	}

	public void comment(Item item) {
		this.getHibernateTemplate().bulkUpdate(
				"update Item i set i.commentCount= i.commentCount+1 where id="
						+ item.getParent().getId());
		this.getHibernateTemplate().save(item);
	}

	public void hide(Item item) {
		this.getHibernateTemplate().bulkUpdate(
				"update Item i set i.hide =true where id=" + item.getId());
	}

	public Item get(Integer id) {
		return this.getHibernateTemplate().get(Item.class, id);
	}

	public TankModel findComments(int itemId, int pageNo,int pageSize) {

		TankModel tm=null;
		try{
		 tm = this
				.queryForModelBySQL(
						"select u.name,u.id,u.photo,c.content,c.pubtime from item c ,User u where c.author_id=u.id and c.parent_id = "
								+ itemId, pageNo,pageSize," order by c.id desc ");
		}catch (Exception e) {
			e.printStackTrace();
			
			
		}
		return tm;
	}

	

}
